package com.zt.spring.ioc.lifeCycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @ClassName: Car
 * @Description: Car
 * @Author: tao.zhang1
 * @Date: 2019/9/27 14:24
 * @Version: V1.0
 **/
@Component
public class Car implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean {
    //Seat也是一个简单的bean对象
    private Seat seat;

    public Car(){
        System.out.println("car instance...");
    }

    public Seat getSeat() {
        return seat;
    }

    @Autowired
    public void setSeat(Seat seat) {
        System.out.println("填充属性");
        this.seat = seat;
    }

    /**
     * 自定义的初始化方法
     */
    @PostConstruct
    public void init(){
        System.out.println("car ... init...");
    }

    /**
     * 自定义的销毁方法
     */
    @PreDestroy
    public void detory(){
        System.out.println("car ... detory...");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println(s);
        System.out.println("BeanNameAware...setBeanName()");
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("DisposableBean...setBeanFactory()");
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContextAware...setApplicationContext()");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean...afterPropertiesSet()");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("InitializingBean...destroy()");
    }
}
